package com.sangeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sangeng.domain.ResponseResult;
import com.sangeng.domain.dto.TagListDto;
import com.sangeng.domain.entity.Tag;
import com.sangeng.domain.entity.User;
import com.sangeng.domain.vo.PageVo;
import com.sangeng.mapper.TagMapper;
import com.sangeng.service.TagService;
import com.sangeng.utils.BeanCopyUtils;
import com.sangeng.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.sql.Date;
import java.time.LocalDate;

/**
 * 标签(Tag)表服务实现类
 *
 * @author makejava
 * @since 2024-08-20 21:48:20
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {
    @Override
    public ResponseResult pageTagList(Long pageNum, Long pageSize, TagListDto tagListDto) {
        //查询时使用标签名和备注进行匹配
        LambdaQueryWrapper<Tag> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.hasText(tagListDto.getName()),Tag::getName,tagListDto.getName());
        queryWrapper.eq(StringUtils.hasText(tagListDto.getRemark()),Tag::getRemark,tagListDto.getRemark());
        //分页查询
        Page<Tag> tagPage=new Page<>();
        tagPage.setCurrent(pageNum);
        tagPage.setSize(pageSize);
        page(tagPage,queryWrapper);

        PageVo pageVo=new PageVo(tagPage.getRecords(),tagPage.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public ResponseResult addTag(TagListDto tagListDto) {
        //获取操作用户信息
        Tag tag = BeanCopyUtils.copyBean(tagListDto, Tag.class);
        tag.setCreateTime(Date.valueOf(LocalDate.now()));
        tag.setCreateBy(SecurityUtils.getUserId());
        tag.setUpdateTime(Date.valueOf(LocalDate.now()));
        tag.setUpdateBy(SecurityUtils.getUserId());
        save(tag);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateByTagId(TagListDto tagListDto) {
        //将类型转换
        Tag tag = BeanCopyUtils.copyBean(tagListDto, Tag.class);
        //设置修改人和修改时间
        tag.setUpdateBy(SecurityUtils.getUserId());
        tag.setUpdateTime(Date.valueOf(LocalDate.now()));
        updateById(tag);
        return ResponseResult.okResult();
    }
}
